/*
 * @Descripttion: 
 * @Author: voanit
 * @Date: 2022-06-07 11:33:04
 * @LastEditors: voanit
 * @LastEditTime: 2022-06-08 12:26:01
 */
import { ApiResponse, Channel } from '@/types/data'
import { RootThunkAction, ChannelAction } from '@/types/store'
import request from '@/utils/request'

import { hasToken } from '@/utils/storage'
import { setChannels, getChannels } from '@/utils/storageChannel'

export const getUserChannel = (): RootThunkAction => {
  return async (dispatch, getState) => {
    // console.log(22333444);

    // const res = await request.get<ApiResponse<{ channels: Channel[] }>>(
    //   '/user/channels'
    // )
    // const { channels } = res.data.data
    // console.log('频道', channels)
    // dispatch({
    //   type: 'channel/saveUserChannels',
    //   payload: channels
    // })
    if (hasToken()) {
      const res = await request.get<ApiResponse<{ channels: Channel[] }>>(
        '/user/channels'
      )
      const { channels } = res.data.data
      // console.log('频道', channels)
      setChannels(channels)
      dispatch({
        type: 'channel/saveUserChannels',
        payload: channels
      })
    } else {
      //没有登录
      const channels = getChannels()
      // console.log('频道111', channels)
      if (channels.length > 0) {
        dispatch({
          type: 'channel/saveUserChannels',
          payload: channels
        })
      } else {
        const res = await request.get<ApiResponse<{ channels: Channel[] }>>(
          '/user/channels'
        )
        const { channels } = res.data.data
        // console.log('频道', channels)
        setChannels(channels)
        dispatch({
          type: 'channel/saveUserChannels',
          payload: channels
        })
      }
    }
  }
}

export function changeActive(id: string): ChannelAction {
  return {
    type: 'channel/changeActive',
    payload: id
  }
}

export function getAllChannel(): RootThunkAction {
  return async (dispatch) => {

    const res = await request.get<ApiResponse<{ channels: Channel[] }>>(
      '/channels'
    )
    dispatch({
      type: 'channel/saveAllChannels',
      payload: res.data.data.channels
    })
  }

}

export function addChannel(channel: Channel): RootThunkAction {
  return async (dispatch, getState) => {
    const { userChannels } = getState().channel

    // const res = await request.get<ApiResponse<{ channels: Channel[] }>>(
    //   '/channels'
    // )
    if (hasToken()) {
      await request.patch('/user/channels', {
        channels: [channel]
      })
    } else {
      // 如果没有登录，将频道数据保存到本地
      // 将channels数据保存本地
      setChannels([...userChannels, channel])
    }

    dispatch({
      type: 'channel/saveUserChannels',
      payload: [...userChannels, channel]
    })
  }

}

export function delChannel(id: string): RootThunkAction {
  return async (dispatch, getState) => {
    const { userChannels } = getState().channel

    if (hasToken()) {
      await request.delete('/user/channels', {
        data: {
          channels: [id]
        }
      })
    } else {
      // eslint-disable-next-line eqeqeq
      // console.log(userChannels.filter(item => item.id != id), id, 122);
      // 如果没有登录，将频道数据保存到本地
      // 将channels数据保存本地
      // eslint-disable-next-line eqeqeq
      setChannels(userChannels.filter(item => item.id != id))
    }

    dispatch(getUserChannel())
  }

}